package dao;

import java.util.List;
import models.Course;
import models.Employee;
import models.Page;
import models.PrivateMessage;
import models.Student;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 * This Dao is for the search function on the site; Here are all finders
 * presented where you can find matches with any of the entered keywords.
 *
 * @author Sander
 */
public class SearchDao {

    private final Session session;

    public SearchDao(Session session) {
        this.session = session;
    }

    /**
     * Searches in all the Courses with the given keywords and returns a list
     * with matches.
     *
     * @param keywords
     * @return
     */
    public List<Course> findCourses(String[] keywords) {
        String where = new String();
        for (int i = 0; i < keywords.length; i++) {
            if (i > 0) {
                where += " OR";
            }
            where += " UPPER(name) LIKE '%" + keywords[i].toUpperCase() + "%'";
            where += " OR";
            where += " UPPER(description) LIKE '%" + keywords[i].toUpperCase() + "%'";
        }
        Transaction tx = session.beginTransaction();
        List<Course> list = session.createQuery("from Course WHERE" + where).list();
        tx.commit();
        return list;
    }

    /**
     * Searches in all the Students with the given keywords and returns a list
     * with matches.
     *
     * @param keywords
     * @return
     */
    public List<Student> findStudents(String[] keywords) {
        String where = "(";
        for (int i = 0; i < keywords.length; i++) {
            if (i > 0) {
                where += " OR";
            }
            where += " UPPER(firstname) LIKE '%" + keywords[i].toUpperCase() + "%'";
            where += " OR";
            where += " UPPER(lastname) LIKE '%" + keywords[i].toUpperCase() + "%'";
            where += " OR";
            where += " UPPER(interests) LIKE '%" + keywords[i].toUpperCase() + "%'";
        }
        where += ") AND hidden = FALSE";
        Transaction tx = session.beginTransaction();
        List<Student> list = session.createQuery("from Student WHERE" + where).list();
        tx.commit();
        return list;
    }

    /**
     * Searches in all the Employees with the given keywords and returns a list
     * with matches.
     *
     * @param keywords
     * @return
     */
    public List<Employee> findTeachers(String[] keywords) {
        String where = new String();
        for (int i = 0; i < keywords.length; i++) {
            if (i > 0) {
                where += " OR";
            }
            where += " UPPER(firstname) LIKE '%" + keywords[i].toUpperCase() + "%'";
            where += " OR";
            where += " UPPER(lastname) LIKE '%" + keywords[i].toUpperCase() + "%'";
            where += " OR";
            where += " UPPER(interests) LIKE '%" + keywords[i].toUpperCase() + "%'";
            where += " OR";
            where += " UPPER(description) LIKE '%" + keywords[i].toUpperCase() + "%'";
        }
        Transaction tx = session.beginTransaction();
        List<Employee> list = session.createQuery("from Employee WHERE" + where).list();
        tx.commit();
        return list;
    }

    /**
     * Searches in all the Pages with the given keywords and returns a list with
     * matches.
     *
     * @param keywords
     * @return
     */
    public List<Page> findPages(String[] keywords) {
        String where = new String();
        for (int i = 0; i < keywords.length; i++) {
            if (i > 0) {
                where += " OR";
            }
            where += " UPPER(title) LIKE '%" + keywords[i].toUpperCase() + "%'";
            where += " OR";
            where += " UPPER(content) LIKE '%" + keywords[i].toUpperCase() + "%'";
        }
        Transaction tx = session.beginTransaction();
        List<Page> list = session.createQuery("from Page WHERE" + where).list();
        tx.commit();
        return list;
    }

    /**
     * Searches in all the Private Messages with the given keywords and returns
     * a list with matches.
     *
     * @param keywords
     * @param owner
     * @return
     */
    public List<PrivateMessage> findPrivateMessages(String[] keywords, long owner) {
        String where = "(";
        for (int i = 0; i < keywords.length; i++) {
            if (i > 0) {
                where += " OR";
            }
            where += " UPPER(subject) LIKE '%" + keywords[i].toUpperCase() + "%'";
            where += " OR";
            where += " UPPER(message) LIKE '%" + keywords[i].toUpperCase() + "%'";
        }
        where += ") AND owner = '" + owner + "'";
        Transaction tx = session.beginTransaction();
        List<PrivateMessage> list = session.createQuery("from PrivateMessage WHERE" + where).list();
        tx.commit();
        return list;
    }
}
